//
//  YHPictureView.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/6/30.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit

//重用cell标示
private let reuseCellId = "pictrueViewId"
//cell的间距
private let itemMargin: CGFloat = 5
//计算cell的宽高
private let cellWH = (SCREENW - StautsMargin*2 - itemMargin*2) / 3

class YHPictureView: UICollectionView {
    //供外界赋值
    var pic_urls: [YHPictureInfo]?{
        didSet{
            let size = getPictureViewSize(pic_urls?.count ?? 0)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            reloadData()
        }
    }
    
    //通过多少张配图计算配图size
    func getPictureViewSize(count:Int) -> CGSize{
        
        //计算行数和列数
        let col = count == 4 ? 2: (count >= 3 ? 3 : count)
        let row = count == 4 ? 2: ((count - 1) / 3 + 1)
        //计算配图高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*itemMargin
        
        return CGSize(width: w, height: h)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout:UICollectionViewFlowLayout())
        setupUI()
        
    }
    
    func setupUI(){
        backgroundColor = UIColor.clearColor()
        //注册cell
        registerClass(YHPictureViewCell.self, forCellWithReuseIdentifier: reuseCellId)
        
        dataSource = self
        //设置流式布局
        let layout = collectionViewLayout as!UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(cellWH, cellWH)
        
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension YHPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func  collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCellId, forIndexPath: indexPath) as! YHPictureViewCell
//        cell.backgroundColor = RandomColor()
        cell.picInfo = pic_urls![indexPath.item]
        return cell
    }
}

//自定义cell
class YHPictureViewCell:UICollectionViewCell{
    //供外界赋值
    var picInfo: YHPictureInfo?{
        didSet{
            bgImageView.sd_setImageWithURL(NSURL(string: picInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        contentView.addSubview(bgImageView)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    }
    
    private lazy var bgImageView:UIImageView = {
        let img = UIImageView(imgName: "timeline_card_top_background")
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
}





